package com.lxr.activity;
import java.io.File;
import com.wxq.draw.DrawDBTool;
import com.wxq.schoolmap.R;

import android.app.Activity; 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle; 
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.Button;
import android.widget.ImageView; 
 
public class PictureActivity extends Activity { 
    private ImageView img1; 
   private Matrix matrix=new Matrix();
    private Matrix savedMatrix=new Matrix();
    private int mode=0 ,lastX, lastY ;
    private float x,y,oldDist = 0,newDist,midx=200,midy=150;
    private Button del,retn;
    private String path,ID;
    private int position;
    private DrawDBTool dbTool;
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.picture); 
        
        del=(Button)findViewById(R.id.delect);
        retn=(Button)findViewById(R.id.retn);
        img1 = (ImageView) findViewById(R.id.imageView1); 
       // img2 = (ImageView) findViewById(R.id.imageView2); 
        retn.setOnClickListener(btnDoSugOnClick);
        del.setOnClickListener(btnDoSugOnClick);
        
        Matrix mt=img1.getImageMatrix();   
        mt.postRotate(-90,150,200);   
        mt.postTranslate(50, 240);
        img1.setImageMatrix(mt);  
       // img2.setImageMatrix(mt); 
        
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        path=bundle.getString("path");
        position=bundle.getInt("position");
        ID=bundle.getString("ID");
        
        Log.d("lxr",path );
        Log.d("lxr",position+"");
        Log.d("lxr",ID );
        
        
        dbTool = new DrawDBTool(this);
        dbTool.open();
        
        BitmapFactory.Options option = new BitmapFactory.Options();
        option.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(path,option);
        option.inSampleSize = option.outWidth/500;        
        //option.inSampleSize = 4;
		option.inJustDecodeBounds = false;
		option.inPreferredConfig = Bitmap.Config.ARGB_8888;
		option.inPurgeable = true;
		option.inInputShareable = true;
		try {
		    bitmap = BitmapFactory.decodeFile(path,option);
			img1.setImageBitmap(bitmap);
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        }
        getResources().getDisplayMetrics(); 
        img1.setOnTouchListener(movingEventListener); 
    } 
    
  
    
    private Button.OnClickListener btnDoSugOnClick=new Button.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v.getId()==R.id.delect){
				dialogShowOK();}
			if(v.getId()==R.id.retn){
				finish();
			}
		}};
		
		private void dialogShowOK() {  

		    new AlertDialog.Builder(this)  
		    .setTitle("删除照片")  
		    .setMessage("您确定要删除文件“"+path+"”吗？")  
		    .setPositiveButton  
		            ("删除", new DialogInterface.OnClickListener()  
		                { public void onClick(DialogInterface dialog, int which) 
		                    {       
		                	        File delFile=new File(path);//执行删除，或者什么。。。操作  
		                           if(delFile.exists())    
		                            delFile.delete();
		                            dbTool.delectimage(ID, position);
		                            finish();
		                    }    
		                }  
		            )
		    .setNegativeButton  
		            ("取消", new DialogInterface.OnClickListener()  
		                { public void onClick(DialogInterface dialog, int which){ }}  )  
		    .show();  
		}  

 
    private OnTouchListener movingEventListener = new OnTouchListener() { 

        public boolean onTouch(View v, MotionEvent event) { 
        	switch (event.getAction() & MotionEvent.ACTION_MASK) { 
            case MotionEvent.ACTION_DOWN: 
                lastX = (int) event.getRawX(); 
                lastY = (int) event.getRawY();
                matrix.set(img1.getImageMatrix());  
                savedMatrix.set(matrix);  
               if(mode==0)
                mode=1;
                break; 
            case MotionEvent.ACTION_POINTER_DOWN:  //多点触控   
                x = event.getX(0) - event.getX(1);    
                y = event.getY(0) - event.getY(1);    
                oldDist = FloatMath.sqrt(x * x + y * y);  
                midx=(event.getX(0)+event.getX(1))/2;
                midy=(event.getY(0)+event.getY(1))/2;
                savedMatrix.set(matrix);  	
                mode=2;
            	break;
            case MotionEvent.ACTION_MOVE:                 
            	if(mode==1){
            	matrix.set(savedMatrix);
            	int dx = (int) event.getRawX() - lastX; 
                int dy = (int) event.getRawY() - lastY; 
                int left = v.getLeft() + dx; 
                int top = v.getTop() + dy; 
                matrix.postTranslate(left, top);
            	}
                if(mode==2){
                x = event.getX(0) - event.getX(1);    
                y = event.getY(0) - event.getY(1);    
                newDist = FloatMath.sqrt(x * x + y * y);   
                
                	matrix.set(savedMatrix);  
                float scale=newDist/oldDist;  
                matrix.postScale(scale, scale,midx ,midy );         
                }
                break; 
            case MotionEvent.ACTION_POINTER_UP:
            	mode=0;
                break; 
            case MotionEvent.ACTION_UP: 
            	mode=0;
                break; 
            } 
        	//img2.setImageMatrix(matrix);
        	img1.setImageMatrix(matrix);
            return true; 
        } 
    }; 
    
    @Override
	protected void onDestroy() {
		dbTool.close();
		super.onDestroy();
	}	
} 
